Skip to content

Conversation

senthilengg
Copy link

@senthilengg senthilengg commented Oct 4, 2025

Description (*)

  1. Moving $this->getEntity()->getEntityIdField(); lookup outside of the foreach
  2. Processing the result of select query and preparing [product_id => [attribute_code => value, attribute_code1 => value1]]
  3. Calling _setItemAttributeValue only once per $selectGroups instead of per $selectGroups, per attribute and per product
  4. Return same response as the original function

Fixed Issues (if relevant)

  1. Fixes magento/magento2 Time complexity of _loadAttributes in Eav\Model\Entity\Collection\AbstractCollection increases with number of products in cart and attributes #40216

Manual testing scenarios (*)

  1. Check all areas thats hitting the EAV

Result

Without this PR - Too many calls to _setItemAttributeValue function
image

With PR - Only one call to _setItemAttributeValue function
image

Additional Questions

Since array_replace is memory intensive for large array would it benefit over all or not ?
array union (+) seems to be memory optimised and used here

Copy link

m2-assistant bot commented Oct 4, 2025

Hi @senthilengg. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

Copy link

Failed to run the builds. Please try to re-run them later.

@senthilengg
Copy link
Author

@magento run unit Tests

@senthilengg
Copy link
Author

@magento run Database Compare,Functional Tests CE,Functional Tests EE,Functional Tests B2B,Integration Tests,Magento Health Index,Sample Data Tests CE,Sample Data Tests EE,Sample Data Tests B2B,Static Tests,WebAPI Tests,Semantic Version Checker

@senthilengg senthilengg changed the title #40216-Replacing nested foreach with array_replace to reduce time complexity #40216-Replacing nested foreach with array_union to reduce time complexity Oct 4, 2025
@senthilengg
Copy link
Author

@magento run Functional Tests CE,Static Tests,WebAPI Tests

@senthilengg senthilengg changed the title #40216-Replacing nested foreach with array_union to reduce time complexity #40216-Replacing nested foreach with array union (+) to reduce time complexity Oct 4, 2025
@senthilengg
Copy link
Author

@magento run Functional Tests EE,Functional Tests B2B,Integration Tests

@senthilengg
Copy link
Author

@magento run Functional Tests CE

@senthilengg
Copy link
Author

@magento run WebAPI Tests

@engcom-Hotel engcom-Hotel added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Oct 7, 2025
@github-project-automation github-project-automation bot moved this to Pending Review in Pull Requests Dashboard Oct 7, 2025
@senthilengg
Copy link
Author

@magento run WebAPI Tests

@senthilengg
Copy link
Author

@magento run WebAPI Tests

@senthilengg
Copy link
Author

@magento run all tests

@engcom-Hotel
Copy link
Contributor

@magento run all tests

@engcom-Hotel engcom-Hotel self-requested a review October 14, 2025 06:42
@engcom-Hotel engcom-Hotel moved this from Pending Review to Review in Progress in Pull Requests Dashboard Oct 14, 2025
Copy link
Contributor

@engcom-Hotel engcom-Hotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @senthilengg,

Thank you for your contribution!

Please have a look into the below review comments.

* @return $this
* @throws LocalizedException
*/
protected function _setItemAttributeValue($valueInfo)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method signature behavior has changed. Any plugin/extension overriding _setItemAttributeValue() will break.

Recommendations:

  • Create a new method _setItemAttributeValues() (plural) for batch processing
  • Keep _setItemAttributeValue() (singular) unchanged for BC
  • Add an automated test for the new method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@engcom-Hotel I think you didn't notice its a protected function in an abstract class. Let me know if you still think we need to introduce new function for other reasons. I couldn't think of a scenario someone wants to override the under the hood Eav Object processing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @senthilengg,

Thanks for your reply!

but I am still thinks that this is the BiC change. Please go through with the below link:

https://developer.adobe.com/commerce/contributor/guides/code-contributions/backward-compatibility-policy/#modifying-the-default-values-of-optional-arguments-in-public-and-protected-methods

and let me know If I missed anything here.

Copy link
Author

@senthilengg senthilengg Oct 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@engcom-Hotel Thank you for sharing the link. Its done!

__('A header row is missing for an attribute. Verify the header row and try again.')
);
}
// Run only once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: If it "runs only once" why is there a foreach loop? This suggests _itemsById[$entityId] contains multiple objects, contradicting the comment.

Copy link
Author

@senthilengg senthilengg Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@engcom-Hotel Without this loop during my manual functional testing and unit testing is working well and passed, I have tested with configurable, bundle and simple products. However without the loop its failing WebAPI Graphql MTF test so there could be a scenario the test script except this to run in a loop but I couldn't reproduce myself. Tried with Xdebug profile as well during the manual test.

To give you another perspective I couldn't think of a scenario where this _itemsById contains more than one product object under the same Id.

_itemsById was set here

if (isset($this->_itemsById[$object->getId()])) {
and if you notice it was fetched from DB. $object->getId() here being the entity ID from the DB can't be a duplicated. So to me it looks like a legacy code sitting there and push the object to the _itemsById array if the same id exists. And this is the reason why MTF Web API test also expecting something, similar however in real world, I couldn't think of a scenario happens like this and thats the reason for my comment that it will run only once.

In any case if you feel its better to remove the please do so. Because if the object contains multiple it should have affected the stores much more since it would have run O( abc) c being the number of objects inside _itemsById

And Thank you so much for the review. Appreciate it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your analysis @senthilengg. I agrees with your point. But I have suggestion below to change the comment as below:

// _itemsById[$entityId] is always an array (typically with one element)
// Foreach handles edge cases where multiple objects share the same entity ID

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@engcom-Hotel Its done!

* @return $this
* @throws LocalizedException
*/
protected function _setItemAttributeValue($valueInfo)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @senthilengg,

Thanks for your reply!

but I am still thinks that this is the BiC change. Please go through with the below link:

https://developer.adobe.com/commerce/contributor/guides/code-contributions/backward-compatibility-policy/#modifying-the-default-values-of-optional-arguments-in-public-and-protected-methods

and let me know If I missed anything here.

__('A header row is missing for an attribute. Verify the header row and try again.')
);
}
// Run only once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your analysis @senthilengg. I agrees with your point. But I have suggestion below to change the comment as below:

// _itemsById[$entityId] is always an array (typically with one element)
// Foreach handles edge cases where multiple objects share the same entity ID

@senthilengg
Copy link
Author

@magento run Unit Tests, WebAPI Tests, Static Tests

@engcom-Hotel
Copy link
Contributor

@magento run all tests

Copy link
Contributor

@engcom-Hotel engcom-Hotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @senthilengg for making the changes.

Failed tests seems flaky to me, hence approving this PR.

@ct-prd-projects-boards-automation ct-prd-projects-boards-automation bot moved this from Review in Progress to Ready for Testing in Community Dashboard Oct 16, 2025
@engcom-Bravo engcom-Bravo added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Oct 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: ready for testing Project: Community Picked PRs upvoted by the community Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Projects

Status: Ready for Testing

Development

Successfully merging this pull request may close these issues.

Time complexity of _loadAttributes in Eav\Model\Entity\Collection\AbstractCollection increases with number of products in cart and attributes

4 participants